package com.yaocw.baseproject.activity;

import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import androidx.viewpager.widget.ViewPager;

import com.yaocw.baseproject.R;
import com.yaocw.baseproject.adapter.GuideAdapter;
import com.yaocw.baseproject.view.Indicator;

import java.util.ArrayList;

public class GuideActivity extends BaseActivity {
    private int[] mImages;
    private ArrayList<View> mViewList = new ArrayList<View>();
    private Button btnStart;
    private ViewPager vpGuide;
    Indicator idrIndicator;

    @Override

    protected int initLayout() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initView() {
        btnStart = findViewById(R.id.btn_start);
        btnStart.setVisibility(View.INVISIBLE);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                navigateTo(LoginActivity.class);
                finish();
            }
        });

        idrIndicator = findViewById(R.id.idr_indicator);

        vpGuide = findViewById(R.id.vp_guide);
        vpGuide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                idrIndicator.setActiveIdx(position);
                idrIndicator.repaint();
            }

            @Override
            public void onPageSelected(int position) {
                if (position == (mViewList.size() - 1)) {
                    btnStart.setVisibility(View.VISIBLE);
                    btnStart.startAnimation(AnimationUtils.loadAnimation(GuideActivity.this, R.anim.btn_guide_start));
                } else {
                    btnStart.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {
        mImages = new int[]{R.mipmap.guide1, R.mipmap.guide2, R.mipmap.guide3};

        for (int image : mImages) {
            View guideLayout = getLayoutInflater().inflate(R.layout.guide_page, null);
            ImageView ivGuide = guideLayout.findViewById(R.id.iv_guide);
            ivGuide.setImageResource(image);
            mViewList.add(guideLayout);
        }

        vpGuide.setAdapter(new GuideAdapter(mViewList));
    }
}